package com.wxdc.nmgyt.nmgytwydc.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.MassTransitRouteLine;
import com.wxdc.nmgyt.nmgytwydc.R;

import java.util.List;

/**
 * Created by Administrator on 2018/7/31.
 */

public class RouteLineAdapter extends BaseAdapter {
    private List<? extends  RouteLine> routeLines;
    private LayoutInflater layoutInflater;
    private Type mtype;

    public RouteLineAdapter(Context context,List<? extends RouteLine> routeLines, Type mtype) {
        this.routeLines = routeLines;
        layoutInflater = LayoutInflater.from(context);
        this.mtype = mtype;
    }

    @Override
    public int getCount() {
        return routeLines.size();
    }

    @Override
    public Object getItem(int position) {
        return routeLines.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NodeViewHolder nodeViewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.activity_transit_item, null);
            nodeViewHolder = new NodeViewHolder();
            nodeViewHolder.name = (TextView) convertView.findViewById(R.id.transitName);
            nodeViewHolder.lightNum = (TextView) convertView.findViewById(R.id.lightNum);
            nodeViewHolder.dis = (TextView) convertView.findViewById(R.id.dis);
            convertView.setTag(nodeViewHolder);
        }else {
            nodeViewHolder = (NodeViewHolder) convertView.getTag();
        }
        switch (mtype) {
            case  TRANSIT_ROUTE:
            case WALKING_ROUTE:
            case BIKING_ROUTE:
                nodeViewHolder.name.setText("路线" + (position + 1));
                int time = routeLines.get(position).getDuration();
                if ( time / 3600 == 0 ) {
                    nodeViewHolder.lightNum.setText( "大约需要：" + time / 60 + "分钟" );
                } else {
                    nodeViewHolder.lightNum.setText( "大约需要：" + time / 3600 + "小时" + (time % 3600) / 60 + "分钟" );
                }
                nodeViewHolder.dis.setText("距离大约是：" + routeLines.get(position).getDistance() + "米");
                break;

            case DRIVING_ROUTE:
                DrivingRouteLine drivingRouteLine = (DrivingRouteLine) routeLines.get(position);
                nodeViewHolder.name.setText( "线路" + (position + 1));
                nodeViewHolder.lightNum.setText( "红绿灯数：" + drivingRouteLine.getLightNum());
                nodeViewHolder.dis.setText("拥堵距离为：" + drivingRouteLine.getCongestionDistance() + "米");
                break;
            case MASS_TRANSIT_ROUTE:
                MassTransitRouteLine massTransitRouteLine = (MassTransitRouteLine) routeLines.get(position);
                nodeViewHolder.name.setText("线路" + (position + 1));
                nodeViewHolder.lightNum.setText( "预计达到时间：" + massTransitRouteLine.getArriveTime() );
                nodeViewHolder.dis.setText("总票价：" + massTransitRouteLine.getPrice() + "元");
                break;

            default:
                break;

        }

        return convertView;
    }

    //-----------------------------------------------------------------------
    private class NodeViewHolder {
        private TextView name;
        private TextView lightNum;
        private TextView dis;
    }
    //----------------------------------------------------------------------
    public enum Type {
        MASS_TRANSIT_ROUTE, // 综合交通
        TRANSIT_ROUTE, // 公交
        DRIVING_ROUTE, // 驾车
        WALKING_ROUTE, // 步行
        BIKING_ROUTE // 骑行

    }
}
